package com.xiaofeng.auth.domain.service.impl;

import com.xiaofeng.auth.common.enums.IsDeletedFlagEnum;
import com.xiaofeng.auth.domain.entity.AuthRolePermissionBO;
import com.xiaofeng.auth.domain.service.AuthRolePermissionDomainService;
import com.xiaofeng.subject.infra.basic.entity.AuthRolePermission;
import com.xiaofeng.subject.infra.basic.service.AuthRolePermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: chc
 * @Date: 2024/02/25/13:52
 * @Description:
 */
@Service
@Slf4j
public class AuthRolePermissionDomainServiceImpl implements AuthRolePermissionDomainService{

    @Resource
    private AuthRolePermissionService AuthRolePermissionService;

    @Override
    public Boolean add(AuthRolePermissionBO authRolePermissionBO) {
        //创建一个集合
        List<AuthRolePermission> rolePermissionList = new LinkedList<>();
        //获取角色Id
        Long roleId = authRolePermissionBO.getRoleId();
        //循环遍历权限id列表集合
        authRolePermissionBO.getPermissionIdList().forEach(permissionId -> {
            AuthRolePermission authRolePermission = new AuthRolePermission();
            authRolePermission.setRoleId(roleId);//设置角色Id
            authRolePermission.setPermissionId(permissionId);//设置权限Id
            authRolePermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());//设置删除状态
            rolePermissionList.add(authRolePermission);//将每次数据添加到集合中
        });
        //批量插入数据库
        int count = AuthRolePermissionService.batchInsert(rolePermissionList);
        return count > 0;
    }
}
